package com.lohit.dbtest.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.R;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.lohit.dbtest.DbController;
import com.lohit.dbtest.Main;
import com.lohit.dbtest.ScheduleAppActivity;

/**
 * This service is started when an Alarm has been raised
 * 
 * We pop a notification into the status bar for the user to click on When the
 * user clicks the notification a new activity is opened
 * 
 * @author paul.blundell
 */
@SuppressLint("SimpleDateFormat")
public class NotifyService extends Service {

	public static final String PREFS_APPOINTMENTS = "AppointmentDetails";

	// DbController controller = new DbController(getApplicationContext());

	/**
	 * Class for clients to access
	 */
	public class ServiceBinder extends Binder {
		NotifyService getService() {
			return NotifyService.this;
		}
	}
	
	
	// Unique id to identify the notification.
	private static final int NOTIFICATION = 123;
	// Name of an intent extra we can use to identify if this service was
	// started to create a notification
	public static final String INTENT_NOTIFY = "com.lohit.dbtest.service.INTENT_NOTIFY";
	// The system notification manager
	private NotificationManager mNM;

	@Override
	public void onCreate() {
		
		
		Log.i("NotifyService", "onCreate()");
		
		mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		
		
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i("LocalService", "Received start id " + startId + ": " + intent);

		// If this service was started by out AlarmTask intent then we want to
		// show our notification
		if (intent.getBooleanExtra(INTENT_NOTIFY, false))
			showNotification();

		// We don't care if this service is stopped as we have already delivered
		// our notification
		return START_NOT_STICKY;
	}

	@Override
	public IBinder onBind(Intent intent) {
		return mBinder;
	}

	// This is the object that receives interactions from clients
	private final IBinder mBinder = new ServiceBinder();

	/**
	 * Creates a notification and shows it in the OS drag-down status bar
	 */
	private void showNotification() {

		SharedPreferences app_settings = getSharedPreferences(
				PREFS_APPOINTMENTS, 0);
		String pateintName = app_settings.getString("patientName", null);
		
		String time = app_settings.getString("time", null);
		
		String date = app_settings.getString("date", null);

		
  String dispTime = time ;
		
		try {
			
			
			
			Date temp = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss").parse(date+ " " +time);
			
			
			Log.d("date", temp.toString());
			
			
			SimpleDateFormat timeFormat =new SimpleDateFormat("hh:mm aa");
			
			dispTime = timeFormat.format(temp);
		
			
			
		} catch (ParseException e) {
			
			e.printStackTrace();
			
			Log.d("error", "Display time");
			
			
			
		}
		
		
//		String date = app_settings.getString("date", null);
//		

		
		
		CharSequence title = "Appointment!!";
		// This is the icon to use on the notification
		int icon = R.drawable.ic_dialog_alert;

		// This is the scrolling text of the notification
		CharSequence text = "You have an appointment with " + pateintName+ " at "+dispTime;
		// What time to show on the notification
		long time1 =System.currentTimeMillis();

		Notification notification = new Notification(icon, text, time1);

		// The PendingIntent to launch our activity if the user selects this
		// notification
		PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
				new Intent(this, Main.class), 0);

		// Set the info for the views that show in the notification panel.
		notification.setLatestEventInfo(this, title, text, contentIntent);
		notification.defaults = Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND;
		
		
		// Clear the notification when it is pressed
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		// Send the notification to the system.
		mNM.notify(NOTIFICATION, notification);

		// Stop the service when we are finished
		stopSelf();
	}
}